/*
 * Created on 22.10.2003
 * Local Projects Database
 * Mihai Postelnicu (mihai@ro-gateway.org)
 * (c) 2003 by eRomania Gateway
 */
package ro.gateway.aida.obj;

import java.lang.reflect.Field;

import ro.gateway.aida.servlet.Constants;
import ro.gateway.aida.utils.Localizable;

/**
 * @author Mihai Postelnicu Defines beans that can be showed on a localized
 *         interface (web page) This object is managed by
 *         Localised(Sortable)List e-mail (mihai@ro-gateway.org) (c) 2003 by
 *         eRomania Gateway
 */
public abstract class LocalizedItem implements Localizable {
	protected String viewLang;

	public LocalizedItem() {
		viewLang = Constants.DEFAULT_LANGUAGE;
	}

	/**
	 * @return
	 */
	public String getViewLang() {
		return viewLang;
	}

	/**
	 * Identifies the Localizable property items of the current class and sets
	 * the view language. The LangString will become available as a normal
	 * string, by using the toString() method A viewer can display LangString
	 * items as normal strings, if they have a view language set
	 * 
	 * @param string
	 */
	public void setViewLang(String lang) {
		viewLang = lang;
		Field[] fields = this.getClass().getDeclaredFields();

		try {
			for (int i = 0; (fields != null) && (i < fields.length); i++) {
				//current set

				/*
				 * if (fields[i].getType().getName().equals(
				 * "ro.gateway.aida.obj.LangString")) { LangString t =
				 * (LangString) fields[i].get(this);
				 * 
				 * if (t != null) t.setViewLang(lang); } else
				 */
				//nested set
				if ((Class.forName("ro.gateway.aida.utils.Localizable").isAssignableFrom(fields[i].getType()))) {
					Localizable t = (Localizable) fields[i].get(this);
					if (t != null)
						t.setViewLang(lang);
				}

			}

		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
}